package com.smartwasser.yunhtgl.apphtgl.presenter;

import android.os.Handler;

import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.network.RetrofitApi;
import com.smartwasser.yunhtgl.apphtgl.network.Retrofits;
import com.smartwasser.yunhtgl.apphtgl.view.SPGreat1View;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SPGreat1PresenterImpl implements SPGreat1Presenter {
    private SPGreat1View sp1GreatView;
    private Handler mHandler = new Handler();

    public SPGreat1PresenterImpl(SPGreat1View sp1GreatView) {
        this.sp1GreatView = sp1GreatView;
    }

    @Override
    public void getType1Sp1Great(final String JSESSIONID, final String id) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getType1Sp1Great(JSESSIONID, id)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<SPGreat1Bean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                sp1GreatView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final SPGreat1Bean object) {
                Logger.e("JSESSIONID = " + JSESSIONID + "__id" + id);
                sp1GreatView.success(object);

            }
        });
    }

    @Override
    public void getFlMessage(String JSESSIONID) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getFlMessage(JSESSIONID)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<FlMessageBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                sp1GreatView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final FlMessageBean flMessageBean) {
                sp1GreatView.successGetFlMessage(flMessageBean);
            }
        });
    }

    @Override
    public void getCommitSH(String JSESSIONID, String id, String flag, String comment, String shouzhiType, String type, String legalOpinionBook) {
        Retrofits.getInstance().createReq(RetrofitApi.class).commitSP(JSESSIONID, id, flag, comment, shouzhiType, type, legalOpinionBook)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BaseBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                sp1GreatView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final BaseBean baseBean) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        sp1GreatView.successCommitSH(baseBean);
                    }
                }, 1000);
                Logger.e("onNext");
            }
        });
    }
}
